package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.StrichenService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.StrichenDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing Strichen.
 */
@RestController
@RequestMapping("/api")
public class StrichenResource {

    private final Logger log = LoggerFactory.getLogger(StrichenResource.class);

    private static final String ENTITY_NAME = "strichen";

    private final StrichenService strichenService;

    public StrichenResource(StrichenService strichenService) {
        this.strichenService = strichenService;
    }

    /**
     * POST  /strichens : Create a new strichen.
     *
     * @param strichenDTO the strichenDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new strichenDTO, or with status 400 (Bad Request) if the strichen has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/strichens")
    @Timed
    public ResponseEntity<StrichenDTO> createStrichen(@RequestBody StrichenDTO strichenDTO) throws URISyntaxException {
        log.debug("REST request to save Strichen : {}", strichenDTO);
        if (strichenDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new strichen cannot already have an ID")).body(null);
        }
        StrichenDTO result = strichenService.save(strichenDTO);
        return ResponseEntity.created(new URI("/api/strichens/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /strichens : Updates an existing strichen.
     *
     * @param strichenDTO the strichenDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated strichenDTO,
     * or with status 400 (Bad Request) if the strichenDTO is not valid,
     * or with status 500 (Internal Server Error) if the strichenDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/strichens")
    @Timed
    public ResponseEntity<StrichenDTO> updateStrichen(@RequestBody StrichenDTO strichenDTO) throws URISyntaxException {
        log.debug("REST request to update Strichen : {}", strichenDTO);
        if (strichenDTO.getId() == null) {
            return createStrichen(strichenDTO);
        }
        StrichenDTO result = strichenService.save(strichenDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, strichenDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /strichens : get all the strichens.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of strichens in body
     */
    @GetMapping("/strichens")
    @Timed
    public ResponseEntity<List<StrichenDTO>> getAllStrichens(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of Strichens");
        Page<StrichenDTO> page = strichenService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/strichens");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /strichens/:id : get the "id" strichen.
     *
     * @param id the id of the strichenDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the strichenDTO, or with status 404 (Not Found)
     */
    @GetMapping("/strichens/{id}")
    @Timed
    public ResponseEntity<StrichenDTO> getStrichen(@PathVariable Long id) {
        log.debug("REST request to get Strichen : {}", id);
        StrichenDTO strichenDTO = strichenService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(strichenDTO));
    }

    /**
     * DELETE  /strichens/:id : delete the "id" strichen.
     *
     * @param id the id of the strichenDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/strichens/{id}")
    @Timed
    public ResponseEntity<Void> deleteStrichen(@PathVariable Long id) {
        log.debug("REST request to delete Strichen : {}", id);
        strichenService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/strichens?query=:query : search for the strichen corresponding
     * to the query.
     *
     * @param query the query of the strichen search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/strichens")
    @Timed
    public ResponseEntity<List<StrichenDTO>> searchStrichens(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of Strichens for query {}", query);
        Page<StrichenDTO> page = strichenService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/strichens");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
